package project.bluetooth.com.loadsirproject.dialog;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;

import project.bluetooth.com.loadsirproject.R;


/**
 * Created by lgs on 2018/4/20.
 */

public class SignInDialogFragment extends DialogFragment {

    private EditText mEtUsername;
    private EditText mEtPassword;

    public interface SignInInputListener {
        void onLoginInputComplete(String username, String password);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.i("DIALOG_LOG", "onCreateView");
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    private void initView(View view) {
        mEtUsername = view.findViewById(R.id.et_txt_username);
        mEtPassword = view.findViewById(R.id.et_txt_password);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Log.i("DIALOG_LOG", "onCreateDialog");
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.fragment_login_dialog, null);

        initView(view);

        builder.setView(view)
                .setPositiveButton("Sign in",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                SignInInputListener listener = (SignInInputListener) getActivity();

                                String username = mEtUsername.getText().toString();
                                String password = mEtPassword.getText().toString();
                                listener.onLoginInputComplete(username, password);

                            }
                        }).setNegativeButton("Cancel", null);

        return builder.create();
    }
}
